#!/bin/bash

# Copyright (c) 2011-2018 LEVAI Daniel
# All rights reserved.
#
# * Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
# * Redistributions of source code must retain the above copyright notice
#   this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in the
#   documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=kc
VERSION=${VERSION:-2.4.2}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ];then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e


# XXX Upgrade incompatibility notice
if [ -f /var/log/packages/kc-*_SBo ];then
	typeset -i KC_MINOR=$( ls -1 /var/log/packages/kc-*_SBo |cut -d- -f2 |cut -d. -f2 )
	if [ ${KC_MINOR} -lt 4 ];then
		echo ""
		echo "!!! Incompatibility notice !!!"
		echo ""
		echo "This SlackBuild has detected a version of kc older than 2.4 on your system."
		echo "Starting with version 2.4, kc uses a newer database format."
		echo "What this means is that starting from kc-2.4, it will not be"
		echo "able to open older databases, and an older kc will not be able"
		echo "to open newer databases."
		echo "The recommended upgrade path is detailed in the kc-2.4 package's"
		echo "Changelog file:"
		echo "https://github.com/levaidaniel/kc/blob/2.4/Changelog"
		echo "... but here is a shallow outline of the procedure:"
		echo "1) Dump your database as an XML file with your old kc."
		echo "   See the dump command's description."
		echo "2) Upgrade kc to version 2.4 or later."
		echo "3) Create a new empty database."
		echo "4) Import your XML dump into your empty database."
		echo "5) Shred your XML export."
		echo ""
		echo "But really, you should check the Changelog file for detailed instructions."
		echo ""
		echo -n "Type in UPGRADE, if you are ready to upgrade: "; read

		if [ "${REPLY}" != 'UPGRADE' ];then
			echo "Not ready to upgrade!"
			exit 1;
		fi
	fi
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;


if [ -n "${LIBSCRYPT}" ];then
	LIBSCRYPT="HAVE_LIBSCRYPT=y"
fi

if [ -n "${EDITLINE}" ];then
	EDITLINE="EDITLINE=y"
fi

CFLAGS="$SLKCFLAGS" make -f Makefile.linux HAVE_PCRE=y ${LIBSCRYPT} ${EDITLINE}
make -f Makefile.linux install DESTDIR=$PKG PREFIX=/usr

strip --strip-unneeded $PKG/usr/bin/kc
gzip -9 $PKG/usr/man/man1/kc.1

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a Changelog LICENSE $PKG/usr/doc/$PRGNAM-$VERSION/
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
